# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from vcbstudio.items import VcbstudioItem
from vcbstudio.loader import SourceLoader
from scrapy.loader import ItemLoader


class SourceSpider(CrawlSpider):
    name = 'source'
    allowed_domains = ['vcb-s.com']
    start_urls = ['https://vcb-s.com/']

    rules = (
        Rule(LinkExtractor(allow='archives\/.*', restrict_css='#article-list .title-article a'), callback='parse_item'),
        Rule(LinkExtractor(restrict_css='.pagination li:last-child a'))
    )

    def parse_item(self, response):
        item = {}
        #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
        #item['name'] = response.xpath('//div[@id="name"]').get()
        #item['description'] = response.xpath('//div[@id="description"]').get()

        loader = ItemLoader(item=VcbstudioItem(), response=response)
        loader.add_css('title', '.title-article h1 a::text')
        loader.add_css('date', '.label i.fa.fa-calendar::text')
        loader.add_css('image', '.thumbnail img::attr("src")')
        loader.add_css('content', '.centent-article p::text')
        loader.add_css('source_url', '.dw-box.dw-box-download p a::attr("href")')
        yield loader.load_item()
